<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\DataDeliveryProductCount;
use App\Models\Orm\DataProductCount;
use App\Models\Orm\DataUserCount;
use Illuminate\Support\Facades\DB;

/**
 * 产品数据统计工厂方法
 *
 * Class DataProductCountFactory
 * @package App\Models\Factory
 */
class DataDeliveryFactory extends AbsModelFactory
{
    /**
     * 获取单日产品总览
     * @return array
     */
    public static function getDayDataDeliveryCount($date)
    {
        $res = DataDeliveryProductCount::select([
            DB::raw('sum(data_delivery_product_count.channel_id ) as channel'),
            'delivery.title',
            DB::raw('sum(data_delivery_product_count.apply_total) as apply_total'),
            DB::raw('sum(data_delivery_product_count.user_total) as user_total'),
            DB::raw('sum(data_delivery_product_count.xduser_total) as xjd_total '),
            DB::raw('sum(data_delivery_product_count.awuser_total) as aw_total'),

        ])->leftjoin('delivery', 'delivery.id', '=', 'data_delivery_product_count.channel_id')
            ->where('data_delivery_product_count.create_date', $date)
            ->groupby('data_delivery_product_count.channel_id')
            ->get();

        return $res ?: [];
    }

    /**
     * 获取30天产品总览
     * @return array
     */
    public static function getMonthDataDeliveryCount()
    {
        $start = date('Y-m-d', strtotime('-31 day'));
        $end = date('Y-m-d', strtotime('-1 day'));

        $res = DataDeliveryProductCount::select([
            DB::raw('sum(data_delivery_product_count.xduser_total) as xduser_total'),
            DB::raw('sum(data_delivery_product_count.awuser_total) as awuser_total'),
            DB::raw('sum(data_delivery_product_count.apply_total) as apply_total'),
            DB::raw('sum(data_delivery_product_count.user_total) as user_total'),
            'delivery.nid',
            'delivery.title',
            'data_delivery_product_count.channel_id'
        ])->leftjoin('delivery', 'delivery.id', '=', 'data_delivery_product_count.channel_id')
            ->where('create_date', '>', $start)
            ->where('create_date', '<=', $end)
            ->groupby('data_delivery_product_count.channel_id')
            ->get();

//        dd($res);
        return $res ?: [];
    }

    /**
     * 获取某产品统计列表
     *
     * @param $product_id
     * @return mixed
     */
    public static function getDeliveryCountList($channel)
    {
        $res = DataDeliveryProductCount::select('*')
            ->where('channel_id', $channel)
            ->get();

        return $res;
    }

}
